package com.jiezuo.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @description   用于对象与实体类的转换
* @author        庞留杰
* @date          2020/7/31 9:39
* @version       V1.0
**/
public class JsonUtils {
    /**
     * 判断字符串是否为json格式
     * @param content
     * @return
     */
    public static boolean isJson(String content) {
        if(StringUtils.isEmpty(content)){
            return false;
        }
        boolean isJsonObject = true;
        boolean isJsonArray = true;
        try {
            JSONObject.parseObject(content);
        } catch (Exception e) {
            isJsonObject = false;
        }
        try {
            JSONObject.parseArray(content);
        } catch (Exception e) {
            isJsonArray = false;
        }
        if(!isJsonObject && !isJsonArray){ //不是json格式
            return false;
        }
        return true;
    }

    public static String objToJson(Object obj){
        //应对出现重复双引号: ""xxx""
        if(obj instanceof String){
            return obj.toString();
        }else if(!StringUtil.isNotEmpty(obj+"")){
            return null;
        }
        return JSON.toJSONString(obj);
    }

    public static <T> T jsonToObj(String jsonStr, Class<T> clazz){
        return JSON.parseObject(jsonStr, clazz);
    }

    public static <T> Map<String, Object> jsonToMap(String jsonStr){
        return JSON.parseObject(jsonStr, Map.class);
    }

    public static <T> T mapToObj(Map<?, ?> map, Class<T> clazz){
        return JSON.parseObject(JSON.toJSONString(map), clazz);
    }

    public static Map<String,Object> objToMap(Object obj){
       return JSON.parseObject(JSON.toJSONString(obj),new TypeReference<Map<String,Object>>(){});
    }

    public static <T> List<T> jsonToList(String jsonStr, Class<T> clazz){
        return JSON.parseArray(jsonStr, clazz);
    }

    public static <T> List<T> objToList(Object obj, Class<T> clazz){
        return JSON.parseArray(JSON.toJSONString(obj), clazz);
    }

    public static List<Map<String, Object>> jsonToListMap(String json){
        List<Object> list =JSON.parseArray(json);

        List< Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
        for (Object object : list){
           // Map<String,Object> ageMap = new HashMap<String,Object>();
            Map <String,Object> map = (Map<String, Object>) object;//取出list里面的值转为map
            listMap.add(map);
        }
        return listMap;

    }

    /**
     * 把Object数组转换成指定对象的数组
     * @param targetType
     * @param arrayObjects
     * @param <T>
     * @return
     */
    public static <T> T[] convertArray(Class<T> targetType, Object[] arrayObjects) {
        if (targetType == null) {
            return (T[]) arrayObjects;
        }
        if (arrayObjects == null) {
            return null;
        }
        T[] targetArray = (T[]) Array.newInstance(targetType, arrayObjects.length);
        try {
            System.arraycopy(arrayObjects, 0, targetArray, 0, arrayObjects.length);
        } catch (ArrayStoreException e) {
            e.printStackTrace();
        }
        return targetArray;
    }

}